package com.tekj.cxqcb.view.Module.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.SelectInviteListBean;
import com.tekj.cxqcb.view.Module.adapter.InvitationRecordAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;

/**
 * 邀请记录
 */
public class InvitationRecordActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.rv_record)
    RecyclerView rvRecord;
    @BindView(R.id.srl_main)
    SwipeRefreshLayout srlMain;

    InvitationRecordAdapter adapter;
    BusinessDao businessDao;
    int page = 1;

    @Override
    public void init() {
        tvTitle.setText("邀请记录");
        businessDao = new BusinessDao(this, this);
        adapter = new InvitationRecordAdapter(null);

        rvRecord.setLayoutManager(new LinearLayoutManager(activity));
        rvRecord.setAdapter(adapter);

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                businessDao.SelectInviteList(MainBActivity.User.getUserId(), page);
            }
        }, rvRecord);

        adapter.setEmptyView(R.layout.rv_empty);
        srlMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                businessDao.SelectInviteList(MainBActivity.User.getUserId(), page);
            }
        });
        businessDao.SelectInviteList(MainBActivity.User.getUserId(), page);

    }

    @Override
    public int setLayout() {
        return R.layout.activity_invitation_record;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 拉新记录:
                SelectInviteListBean inviteListBean = (SelectInviteListBean) bindingViewBean.getBean();
                if (inviteListBean.getCode().equals("0000000")) {
                    if (page == 1) {
                        adapter.setNewData(inviteListBean.getData().getList());
                    } else {
                        adapter.addData(inviteListBean.getData().getList());
                    }
                    adapter.loadMoreComplete();
                    if (inviteListBean.getData().getList().size() < 10) {
                        adapter.loadMoreEnd();
                    }
                }
                break;
        }

        if (srlMain != null) {
            srlMain.setRefreshing(false);
        }
    }


    @OnClick(R.id.ll_back)
    public void onViewClicked() {
        finish();
    }
}
